<?php

namespace App\Http\Controllers\Manage;

use App\Model\Product;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;

use App\Model\ProductCategory;
use OSS\Core\OssException;
class ProductCategoryController extends Controller
{
    /**
     * //定义一个数组  用来排列查询选项或列表头
     * @var array
     */
    protected static $field = [
        ['id'=>1,'navigation'=>'编号'],
        ['id'=>2,'navigation'=>'权重'],
        ['id'=>3,'navigation'=>'更新时间'],
        ['id'=>4,'navigation'=>'分类名'],
        ['id'=>5,'navigation'=>'父级分类'],
        ['id'=>6,'navigation'=>'分类级别'],
        ['id'=>7,'navigation'=>'状态'],
    ];

    /**
     * //产品分类列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function cateList() {
        #产品搜索条件
        $type = Input::get('type');
        $param = Input::get('param');
        $page = Input::get('page');
        $count = Input::get('count');
        $obj = new ProductCategory();
        if($type == 1){
            $obj = $obj->where('id','like',"%$param%");
        }elseif ($type == 2){
            $obj = $obj->where('rank','like',"%$param%");
        }elseif ($type == 3){
            $obj = $obj->where('update_time','like',"%$param%");
        }elseif ($type == 4){
            $obj = $obj->where('name','like',"%$param%");
        }elseif ($type == 5){
            if($param == '无'){
                $obj = $obj->where('level',1);
            }else{
                $c = ProductCategory::where('name','like',"%$param%")->where('level',1)->get();
                $id = array();
                foreach ($c as $k=>$v){
                    $id[] = $v->id;
                }
                $obj = $obj->whereIn('parent_id',$id);
            }
        }elseif ($type == 6){
            $level = array();
            if(strpos('一级分类',$param) !== false)$level[] = 1;
            if(strpos('二级分类',$param) !== false)$level[] = 2;
            $obj = $obj->whereIn('level',$level);
        }
        $cate = $obj->orderBy('rank','DESC')
            ->orderBy('level','ASC')
            ->orderBy('id','ASC')
            ->paginate(10);
        if(empty($page)){
            $count = $obj->count();
        }
        $navigation = self::$field;
        return view('manage.product.cate_list',[
            'cate'=>$cate,
            'navigation'=>$navigation,
            'type'=>$type,
            'param'=>$param,
            'count'=>$count,
            'active1'=>5,
            'active2'=>3
        ]);
    }
    /**
     * 载入到增加产品分类页
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function add() {
        #获取所有一级分类
        $one = ProductCategory::getOneInfo();
        return view('manage.product.product_category_add',['one'=>$one,'active1'=>5,'active2'=>3]);
    }
    /**
     * //分类写入
     */
    public function addProductCategory()
    {
        $res = ProductCategory::addProductCategory(Input::all());
        if ($res) {
            return [
                'error' => 0,
                'msg' => '添加成功！'
            ];
        } else {
            return [
                'error' => 1,
                'msg' => '添加失败！'
            ];
        }
    }
    /**
     * //获取修改分类ID 载入修改页
     * @param $id
     * @param $level
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function update($id,$level) {
        #判断$param为1时 表示该分类为一级分类 2表示为二级分类
        $category = ProductCategory::getCategoryInfo($id);
        #获取所有一级分类信息
        $one = ProductCategory::getOneInfo();
        return view('manage.product.product_category_update',['category'=>$category,'one'=>$one,'level'=>$level,'active1'=>5,'active2'=>3]);
    }
    /**
     * //更新分类
     */
    public function updateProductCategory() {
        #设置oss参数
        $ossClient = $this->oss();
        $before = ProductCategory::getCategoryInfo(Input::get('id'));
        $object = preg_replace('/.*\//','',$before->picture);
        $res = ProductCategory::updateProductCategory(Input::all());
        if($res){
            try {
                $ossClient->deleteObject($this->bucket, $object);
            } catch (OssException $e) {
            }

            return [
                'error'=>0,
                'msg'=>'修改成功！'
            ];
        }else{
            try {
                $ossClient->deleteObject($this->bucket, preg_replace('/.*\//','',Input::get('path')));
            } catch (OssException $e) {
            }
            return [
                'error'=>1,
                'msg'=>'修改失败！'
            ];
        }
    }
    /**
     * //删除分类
     * @param $id
     * @return string
     */
    public function delCate($id) {
        $res = null;
        $map = ProductCategory::where('id',$id)->first();
        if($map['parent_id'] != 0) {
            $del = ProductCategory::where('id',$id)->delete();
            if($del) {
                $res = [
                    'code' => 100,
                    'msg' => '删除成功!'
                ];
            }
            else {
                $res = [
                    'code' => 101,
                    'msg' => '删除失败!'
                ];
            }
        }
        else {
            $res = [
                'code' => 102,
                'msg' => '一级分类，不能删除!'
            ];
        }
        return json_encode($res);
    }
    /**
     * //禁用该分类
     * @return array
     */
    public function disabled() {
        $id = Input::get('id');
        $status = Input::get('status');
        if($status == 0) {
            $del = ProductCategory::where('id',$id)->update(['status'=>0]);
            $msg = '禁用成功！';
        }else{
            $del = ProductCategory::where('id',$id)->update(['status'=>1]);
            $msg = '启用成功！';
        }
        if(!$del){
            return [
                'code'=>101,
                'msg'=>'设置失败！',
            ];
        }
        return [
            'code'=>100,
            'msg'=>$msg
        ];
    }
}
